<!--
 * @name score
 * @description 组件描述
 * @author idouya
 * @email elegentfrog@163.com
 * @date 2025-07-03
 -->

<template>
  <div class="p-score common-bg common-layout">
    <page-title
      :discipline="gameInfo.discipline"
      :event="gameInfo.event"
      :match="gameInfo.match"
      :type="gameInfo.type"
    ></page-title>
    <div class="content">
      <!-- 展示倒计时时钟 -->
      <div class="content-top content-time-out">{{ runTimeResult }}</div>
      <!-- <div class="content-bottom">{{ timeout2 }}</div> -->
    </div>
  </div>
</template>

<script setup>
import useStore from '@/store'
import Config from '@/assets/js/config'
import { useMultiProcRequest } from '@/assets/js/useMultiProcRequest'
import { onBeforeMount } from 'vue'

const { results, fetchForProcs } = useMultiProcRequest()

const gameInfo = ref({
  discipline: '马术',
  match: '',
  event: 'DRESINDV',
  type: '实时成绩',
})

const atheleteInfo = ref({
  order: 1,
  name: '杨舒涵',
  delegation: '中国',
  horseOrder: 201,
  club: '好骑士嘉豪马术',
  horseName: 'Joanna',
  time: '00:00',
})

let timer1 = null
let timer2 = null
const TIMEOUT_MS = 45000
const runTimeResult = ref(null)
const runTime = ref(0)
const time0 = ref(0)
const time1 = ref(0)

const isRunning = ref(false)

let pastRunTimeMs = 0 // 之前已经运行了多少毫秒
let startTimeMs = 0 // 本轮开始时间戳（毫秒）

const timeout1 = ref(45) // 准备时间
const timeout2 = ref(30) // 运行时间

const { proxy } = getCurrentInstance()
const router = useRouter()
const route = useRoute()
const params = ref({})
const procName = ref([])
const commonStore = useStore.useCommonStore()

onUnmounted(() => {
  clearInterval(timer1)
})
onBeforeMount(() => {
  startTimer()
  const lastMsg = commonStore.receivedActionMessages?.slice(-1)[0]
  if (lastMsg && lastMsg.tplCode == 'scb-jump-45') {
    handleMessage(lastMsg)
  }
})
onMounted(async () => {
  params.value = route.query
  procName.value = Config.procNameList[route.name]
  await getData()
})

const getData = async () => {
  if (procName.value.length > 0) {
    await fetchForProcs(procName.value, {
      procName: procName.value[0],
      keyValues: params.value,
    })
  }
}

// 准备时间倒计时
const startTimer = () => {
  timer1 = setInterval(() => {
    if (isRunning.value) {
      const now = new Date().getTime()
      const totalElapsed = pastRunTimeMs + (now - startTimeMs)
      const remainingMs = TIMEOUT_MS - totalElapsed
      runTimeResult.value = (remainingMs / 1000).toFixed(0)
    }
  }, 200)
}

const handleMessage = (msg) => {
  const data = msg?.data
  if (!data) return
  if (data.status == 1) {
    startTimeMs = new Date().getTime()
    pastRunTimeMs = data.runTime // 假设单位是秒
    isRunning.value = true
  } else {
    runTime.value = data.runTime
    isRunning.value = false
  }
}

watchEffect(() => {
  if (results.value?.recordset?.length) {
    gameInfo.value.event = results.value.recordset[0]?.EventCode
    gameInfo.value.match = results.value.recordset[0]?.MatchName
  }
})

watch(
  () => commonStore.receivedActionMessages,
  (newVal, oldVal) => {
    if (newVal.length > 0) {
      const latestMsg = newVal[newVal.length - 1]
      // const data = latestMsg?.data
      handleMessage(latestMsg)
    }
  },
  { deep: true }
)
</script>

<style scoped lang="scss">
@import '@/assets/scss/dressage/score3';

.content {
  .content-time-out {
    font-size: 30rem !important;
  }
}
</style>
